package se.energimyndigheten.lampguiden;

import android.os.Parcel;
import android.os.Parcelable;
import android.util.Log;

import org.json.JSONArray;
import org.json.JSONException;

public class LightSource implements Parcelable {

	public static final String KEY_LIGHTSOURCE = "LightSource";
	public static final int IMAGE_BASE = 0;
	public static final int IMAGE_WIRE = 1;
	public static final int IMAGE_GLOW = 2;
	public static final int IMAGE_RADIANCE = 3;

	private static final int[] IMAGES = new int[] { R.drawable.ls_store27matt, R.drawable.ls_store27klar, R.drawable.ls_store27halo, R.drawable.ls_litene14matt,
			R.drawable.ls_litene14klar, R.drawable.ls_litene14halo, R.drawable.ls_krone14klar, R.drawable.ls_krone14matt, R.drawable.ls_krone14halo, R.drawable.ls_spotstift,
			R.drawable.ls_spotbajonett, R.drawable.ls_halostiftliten, R.drawable.ls_halostiftogla, R.drawable.ls_lysror, R.drawable.ls_lagenergi, R.drawable.ls_paron, R.drawable.ls_led };
	private static final int[] IMAGES_GLOW_WIRE = new int[] { -1, R.drawable.ls_store27klar_trad, -1, -1, R.drawable.ls_litene14klar_trad, -1, R.drawable.ls_krone14klar_trad, -1,
			-1, -1, -1, -1, -1, -1, -1, R.drawable.ls_paron_trad, -1 };
	private static final int[] IMAGES_GLOW = new int[] { R.drawable.ls_store27matt_glod, R.drawable.ls_store27klar_glod, R.drawable.ls_store27halo_glod_inre,
			R.drawable.ls_litene14matt_glod, R.drawable.ls_litene14klar_glod, R.drawable.ls_litene14halo_glod_inre, R.drawable.ls_krone14klar_glod, R.drawable.ls_krone14matt_glod,
			R.drawable.ls_krone14halo_glod_inre, R.drawable.ls_spotstift_glod, R.drawable.ls_spotbajonett_glod, -1, -1, -1, -1, R.drawable.ls_paron_glod, -1 };
	private static final int[] IMAGES_RADIANCE = new int[] { R.drawable.ls_store27halo_glod_yttre, R.drawable.ls_store27halo_glod_yttre, R.drawable.ls_store27halo_glod_yttre,
			R.drawable.ls_litene14halo_glod_yttre, R.drawable.ls_litene14halo_glod_yttre, R.drawable.ls_litene14halo_glod_yttre, R.drawable.ls_krone14halo_glod_yttre,
			R.drawable.ls_krone14halo_glod_yttre, R.drawable.ls_krone14halo_glod_yttre, -1, -1, -1, -1, -1, -1, R.drawable.ls_litene14halo_glod_yttre, -1 };
	private final static String TAG = LightSource.class.getSimpleName();
	
	
	private int mId;
	private int[] mEffects;
	private String mCaption;
	private String mLampType = "LampTypeUndefined";

	private String mName;
	private String mDocument;
	private int mMaxLumen;
	private int mLifeExpetancy;
	private JSONArray mPrices;
	private double mEfficiency;
	private String mType;
	private int mPrice;
	private int mMySavings;
	private int mYears;
	private JSONArray mReplaceWith;
	private String mEnergyClass;
	private int mEnergyRank;
    private int mDefaultPrice;

    public LightSource() {
		mLampType = "LampTypeUndefined";
	}

	public LightSource(String name, String document, int maxLumen, int lifeExpectancy, JSONArray prices, double efficiency, String type, int years, JSONArray replace_with,
			String energyClass, int energyRank) {
		mName = name;
		mDocument = document;
		mMaxLumen = maxLumen;
		mLifeExpetancy = lifeExpectancy;
		mPrices = prices;
		mEfficiency = efficiency;
		mType = type;
		mYears = years;
		mReplaceWith = replace_with;
		mEnergyClass = energyClass;
		mEnergyRank = energyRank;
	}
	
//	public LightSource(int id, String lampTypeString, String caption, int[] effects) {
//		mId = id;
//		mCaption = caption;
//		mLampType = lampTypeString;
//		mEffects = effects;
//	}

	public LightSource(Parcel in) {
		readParcel(in);
	}

	public Boolean hasReplacement(String candidateType) {
		if (candidateType == null)
			return false;
		try {
			for (int i = 0; i < mReplaceWith.length(); i++) {
				String replacement = mReplaceWith.getString(i);
				if (candidateType.equals(replacement))
					return true;
			}
		} catch (JSONException e) {
		}
		return false;
	}

	public int getYears() {
		return mYears;
	}

	public int getEnergyRank() {
		return mEnergyRank;
	}

	public String getDocument() {
		return mDocument;
	}

	/**
	 * @return mEfficiency
	 */
	public double getPower() {
		return mEfficiency;
	}

	public int getDefaultPrice(int lumen) {
		int price = 0;
		try {
			for (int p = 0; p < mPrices.length(); p += 2) {
				int lumenPrice = mPrices.getInt(p);
				int validUpToLumen = 0;
				if (p == 0)
					price = lumenPrice;
				if (p + 1 < mPrices.length())
					validUpToLumen = mPrices.getInt(p + 1);
				else
					return lumenPrice; // Last will do
				if (lumen <= validUpToLumen)
					return lumenPrice;

			}
		} catch (JSONException e) { // Fallback to default
			e.printStackTrace();
		}
		return price;
	}

	public int getPrice() {
		return mPrice;
	}

	public void setPrice(int myPrice) {
		mPrice = myPrice;
	}

	public int getMySavings() {
		return mMySavings;
	}

	public void setMySavings(int mySavings) {
		mMySavings = mySavings;
	}

	public double getEffiency() {
		return mEfficiency;
	}

	public int getMaxLumen() {
		return mMaxLumen;
	}

	public int getLifeExpetancy() {
		return mLifeExpetancy;
	}

	public String getName() {
		return mName;
	}

	@Override
	public String toString() {

		return LightSource.class.getSimpleName() + " : " + mId + " : " + mCaption;
	}

	public int getId() {
		return mId;
	}

	public void setId(int id) {
		mId = id;
	}

	public String getLampType() {
		return mLampType;
	}

	public void setLampType(String lampType) {
		mLampType = lampType;
	}

	public String getCaption() {
		return mCaption;
	}

	public void setCaption(String caption) {
		mCaption = caption;
	}

	public int[] getEffects() {
		return mEffects;
	}

	public String getType() {
		return mType;
	}

	public String getEnergyClass() {
		return mEnergyClass;
	}

	public void setEffects(int[] effects) {
		mEffects = effects;
	}

	public int[] getImages() {
		return new int[] { IMAGES[mId], IMAGES_GLOW_WIRE[mId], IMAGES_GLOW[mId], IMAGES_RADIANCE[mId] };
	}

	@Override
	public int describeContents() {
		return 0;
	}

	public void readParcel(Parcel in) {
		mId = in.readInt();
		mYears = in.readInt();
		mMaxLumen = in.readInt();
		mEnergyRank = in.readInt();
		
		mName = in.readString();
		mCaption = in.readString();
		mLampType = in.readString();
		mType = in.readString();
		mEnergyClass = in.readString();
		mEfficiency = in.readDouble();
		try {
			mPrices = new JSONArray(in.readString());
		} catch (JSONException jsnE) {
			Log.e(TAG, "Could not convert mPrices from parceable jsonarray");
			jsnE.printStackTrace();
		}
		mEffects = in.createIntArray();
	}
	
	@Override
	public void writeToParcel(Parcel dest, int flags) {
		dest.writeInt(mId);
		dest.writeInt(mYears);
		dest.writeInt(mMaxLumen);
		dest.writeInt(mEnergyRank);
		
		dest.writeString(mCaption);
		dest.writeString(mLampType);
		dest.writeString(mEnergyClass);
		dest.writeDouble(mEfficiency);
		
		dest.writeString(mPrices.toString());
		
		dest.writeIntArray(mEffects);
	}

	public static final Creator<LightSource> CREATOR = new Creator<LightSource>() {
		public LightSource createFromParcel(Parcel in) {
			return new LightSource(in);
		}

		public LightSource[] newArray(int size) {
			return new LightSource[size];
		}
	};

    public void setDefaultPrice(int price) {
        mDefaultPrice = price;
    }

    public int getDefaultPrice() {
        return mDefaultPrice;
    }
}